package tp.pr5.gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;

import tp.pr5.Direction;
import tp.pr5.NavigationObserver;
import tp.pr5.PlaceInfo;

public class NavigationPanel extends JPanel implements NavigationObserver {	
	private LogView bot;
	private RobotHeading left;
	private MapView right;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Main constructor
	 */
	public NavigationPanel(){		
		/*
		 * Adds the components in the navigationpanel component
		 */
		this.setLayout(new BorderLayout());
		init();
	}
	/**
	 * Creates the components inside the panel
	 */
	private void init(){
		/*
		 * Creates the first layer panel
		 */
		JPanel top_center = new JPanel();
		top_center.setLayout(new BorderLayout());
		
		
		left = new RobotHeading();
		bot = new LogView();
		right = new MapView(bot);
			/*
			 * Adds the robot image panel
			 */			
			top_center.add(left, BorderLayout.WEST);
			/*
			 * Adds the log section
			 */			
			this.add(bot,BorderLayout.SOUTH);
			/*
			 * Adds the map view panel
			 */			
			top_center.add(right, BorderLayout.CENTER);
			/*
			 * Adds the full content
			 */
			this.add(top_center,BorderLayout.CENTER);
			
	}
	
		
	/**
	 * Returns the map panel
	 */
	public MapView getMap(){
		return this.right;
	}
	@Override
	public void headingChanged(Direction newHeading) {
		this.left.refresh(newHeading);
		setInfoText("Changed Direction.");
	}
	@Override
	public void initNavigationModule(PlaceInfo initialPlace, Direction heading) {
		this.left.refresh(heading);
		this.bot.refresh(initialPlace);
		this.right.initialize();
		setInfoText("Game started");
	}
	@Override
	public void placeHasChanged(PlaceInfo placeDescription) {
		this.bot.refresh(placeDescription);	
		setInfoText("Robot arrived in "+placeDescription.getName());
	}
	@Override
	public void placeScanned(PlaceInfo placeDescription) {
	}
	@Override
	public void robotArrivesAtPlace(Direction heading, PlaceInfo place) {
		this.left.refresh(heading);
		this.bot.refresh(place);
		//this.right.getPlaceCell(place)
		this.right.refreshCellsState(place);
	}
	@Override
	public void unVisitPlace(PlaceInfo lastPlace) {
		PlaceCell pc = this.right.getPlaceCell(lastPlace);
		pc.setActive(false);
		pc.setVisited(false);
		
	}
	public void setInfoText(String text){
		this.bot.setInfoText(text);
	}
}
